#!/usr/bin/env python
# -*- coding: utf-8 -*-
from django.contrib.auth.decorators import user_passes_test
from mail.forms import CircularForm
from shared.helper import render_to_response
from mail.tasks import SendTask

@user_passes_test(lambda u: u.is_staff)
def circular(request):
    """
    Creates and sends a circular 
    """

    # If the form has already been filled in
    if request.method == 'POST':
        form = CircularForm(request.POST)
        if form.is_valid(): # All validation rules pass

            subject = form.cleaned_data['subject']
            content = form.cleaned_data['content']

            SendTask.delay(subject, content)

            return render_to_response(
                                        request,
                                        'mail/circular_success.html',
                                        {
                                            'form': form,
                                            'subject': subject,
                                            'content': content,
                                        }
                                     )
    
    # If the form has yet to be filled in
    else:
        form = CircularForm()
        
    return render_to_response(
                                request,
                                'mail/circular.html',
                                {
                                    'form': form,
                                }
                             )
    

